//
//  Session.h
//  HelloWorld
//
//  Created by Martin Le C. on 5/13/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "ServiceConstants.h"
#import "StatusConstants.h"
#import "NetworkConstants.h"
#import "yahooGroup.h"
#import "YahooIdentity.h"
#import "YMSG9Packet.h"
#import "YahooChatUser.h"
#import "PacketBodyBuffer.h"
#import "ChallengeResponseUtility.h"
#import "PropertyConstants.h"
#import "UserStore.h"
@class MVDirectClientConnection;
@interface Session : NSObject {
	@private NSString *primaryID;				// Primary Yahoo ID: the real account id
	@private NSString *loginID;					// Login Yahoo ID: we logged in under this
	@private NSString *password;				// Yahoo user password
	@private NSString *cookieY;
	@private NSString *cookieT;
	@private UInt32 status;					// Yahoo status (available... etc)
	@private NSString *customStatusMessage;		// Message for custom status
	@private BOOL customStatusBusy;			// Available/Back=f, away=t
	@private NSMutableArray *groups;			// Yahoo user's groups
	@private NSMutableArray *identities;		// Yahoo user's identities
	@private UserStore *userStore;				// Canonical (we hope) set of YahooUser's
	// -----Session
	@private UInt32 sessionStatus;				// Status of session (see StatusConstants)
	@private UInt32 sessionId;				// Holds Yahoo's session id
	// -----I/O
	@private MVDirectClientConnection *network;		// Connection handler concrete
	// -----Threads
	@private BOOL pingThreadQuit;
	@private NSThread* pingThread;			// Send ping every 20 minutes
	// -----Login
	@private BOOL loginOver;				// Marks start/end of logon process
	@private NSString *chatID;					// Current chat identity
	YMSG9Packet* cachePacket;
	id theDelegate;
}
@property(nonatomic, assign) NSString *primaryID;				
@property(nonatomic, assign) NSString *loginID;					
@property(nonatomic, assign) NSString *password;				
@property(nonatomic, assign) NSString *cookieY;
@property(nonatomic, assign) NSString *cookieT;								
@property(nonatomic, assign) UInt32 status;					
@property(nonatomic, assign) NSString *customStatusMessage;		
@property(nonatomic, assign) BOOL customStatusBusy;
// changed 6/6/2009 - groups and users added to the same array: groups
@property(nonatomic, assign) NSMutableArray *groups;		
@property(nonatomic, assign) NSMutableArray *identities;				
// -----Session
@property(nonatomic, assign) UInt32 sessionStatus;				
@property(nonatomic, assign) UInt32 sessionId;				
@property(nonatomic, assign) UserStore *userStore;
// -----I/O
@property(nonatomic, assign) MVDirectClientConnection *network;		
// -----Threads			
@property(nonatomic, assign) BOOL pingThreadQuit;
@property(nonatomic, assign) NSThread* pingThread;
// -----Login
@property(nonatomic, assign) BOOL loginOver;		
@property(nonatomic, assign) YMSG9Packet* cachePacket;					
@property(nonatomic, assign) NSString *chatID;
- (id) initWithDelegate:(id)delegate;
- (id) delegate;
- (void) setDelegate:(id)delegate;
- (void) login:(NSString*)u p:(NSString*)p;
- (void) logout;
- (void) reset;
- (void) sendMessage:(NSString*)to msg:(NSString*)msg;
- (void) sendMessage:(NSString*)to msg:(NSString*)msg yid:(YahooIdentity*)yid;
- (void) refreshStats;
- (YahooIdentity*)getPrimaryIdentity;
- (YahooIdentity*)getLoginIdentity;
-(void)activateIdentity:(YahooIdentity*)yid activate:(BOOL)activate;
//-(void) rejectContact:(NSString*)msg;
-(void) updateFriendsStatus:(YMSG9Packet*)pkt;
-(void)transmitAuth;
-(void)transmitAuthResp:(NSString*)s;
-(void)transmitChatPM:(NSString*)to msg:(NSString*)msg;
-(void)transmitChatPing;
-(void)transmitGroupRename:(NSString*)oldName newName:(NSString*)newName;
-(void)transmitIdActivate:(NSString*)Id;
-(void)transmitIdDeactivate:(NSString*)Id;
-(void)transmitList;
-(void)transmitLogoff;
-(void)transmitMessage:(NSString*)to yid:(NSString*)yid msg:(NSString*)msg;
-(void)transmitPing;
-(void)receiveIdAct:(YMSG9Packet*) pkt;
-(void)receiveIdDeact:(YMSG9Packet*) pkt;
-(void)receiveList:(YMSG9Packet*)pkt;
-(void)receiveLogoff:(YMSG9Packet*)pkt;
-(void)receiveLogon:(YMSG9Packet*) pkt;
-(void)receiveMessage:(YMSG9Packet*) pkt;
-(void)receivePing:(YMSG9Packet*)pkt;
-(void)receiveUserStat:(YMSG9Packet*)pkt;
-(void)sendPacket:(PacketBodyBuffer*)body service:(UInt16)service status:(UInt32)s;
-(void)sendPacket:(PacketBodyBuffer*)body service:(UInt16)service status:(UInt32)s sessionId:(UInt32)si;
-(void)sendPacket:(PacketBodyBuffer*)body service:(UInt16)service;
- (void) sendMessage2:(NSString*)from to:(NSString*)to msg:(NSString*)msg;
-(void)transmitMessage2:(id) target;
-(BOOL)past:(long long)time;
-(void)startThreads;
-(void)closeNetwork;
-(void)checkStatus;
-(YahooIdentity*)identityIdToObject:(NSString*) yid;
-(void) checkIdentity:(YahooIdentity*)yid;
-(YMSG9Packet*)compoundChatLoginPacket:(YMSG9Packet*)pkt;
-(void)pingThreadProcess;
-(void)inputThreadProcess:(YMSG9Packet*)pkt;
-(BOOL)inputThreadProcessError:(YMSG9Packet*) pkt;
-(void)receiveChatPM:(YMSG9Packet*)pkt;
-(void)errorMessage:(YMSG9Packet*)pkt m:(NSString*)m;
-(void)resetData;
-(void)transmitUserStat;
-(void)receiveAuth:(YMSG9Packet*)pkt;
-(void)receiveAccInfo:(YMSG9Packet*)pkt;
-(NSString*)getCRUMB:(NSString*) chal;
-(NSString*)getToken:(NSString*) chal;
- (void) directClientConnection:(MVDirectClientConnection *) connection didReadData:(NSData *) data withTag:(long) tag;
-(YMSG9Packet*)receivePacket: (NSData*)readData;
- (void) _readNextMessage;
- (void) directClientConnection:(MVDirectClientConnection *) connection didWriteDataWithTag:(long) tag;
-(void) _writeNextData:(NSData*) data;
-(void) receiveBudStat: (YMSG9Packet*)pkt;
-(void) receiveBudStatList: (YMSG9Packet*)pkt;
@end
// delegates of Chat Core - :) I am close to complete my last test - I love HUT so much.
@interface NSObject (SessionDelegate)
// delegate for receving PM
- (void) Session:(Session *) session didReceivedPM:(NSString *)from to:(NSString*)to msg:(NSString*)msg;
// delegate for logged in - if logged in sucessfully, Yahoo Servers will send back buddy list.
- (void) Session:(Session *) session didReceivedBuddyList:(BOOL)did;
// delegate for time out - if logged in sucessfully, Yahoo Servers will send back buddy list.
- (void) Session:(Session *) session errorLogin:(NSString*)error;
// delegate for changing status of a buddy
- (void) Session:(Session *) session didReceivedBudStatus:(NSString *)buddyId;
@end